package leetcode.slidwidow;

import java.util.HashSet;

public class Lc3298 {


    public long validSubstringCount(String word1, String word2) {
        if (word1.length() < word2.length()) return 0;
        int sum = word2.length();
        int n = word1.length();
        int[] cnt = new int[26];
        long ans = 0;
        HashSet<Character> set = new HashSet<>();
        for (int i = 0; i < word2.length(); i++) {
            set.add(word2.charAt(i));
            cnt[word2.charAt(i) - 'a']++;
        }
        for (int i = 0, j = 0; j < word1.length(); j++) {
            char c = word1.charAt(j);
            if (set.contains(c)) {
                if (--cnt[c - 'a'] >= 0)
                    sum--;
                if (sum == 0) {//凑齐word2字符了
                    while (sum == 0 && i <= j) {
                        ans += n - j;
                        c = word1.charAt(i);
                        if (set.contains(c)) {
                            if (cnt[c - 'a'] >= 0)sum++;
                            cnt[c - 'a']++;
                        }
                        i++;
                    }
                }
            }
        }
        return ans;
    }

    public static void main(String[] args) {

        Lc3298 lc3298 = new Lc3298();

        String s1 = "bcca";
        String s2 = "abc";

        System.out.println(lc3298.validSubstringCount(s1,s2));


    }
}
